package com.kgc.kmall.managerweb.controller;

import com.kgc.kmall.bean.PmsBaseSaleAttr;
import com.kgc.kmall.bean.PmsProductImage;
import com.kgc.kmall.bean.PmsProductInfo;
import com.kgc.kmall.bean.PmsProductSaleAttr;
import com.kgc.kmall.service.SpuService;
import org.apache.commons.io.FilenameUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
public class SpuController {
    @Reference
    SpuService spuService;

    @RequestMapping("/spuList")
    public List<PmsProductInfo> spuList(Long catalog3Id){
        List<PmsProductInfo> pmsProductInfos = spuService.spuList(catalog3Id);
        return pmsProductInfos;
    }


    @RequestMapping("/fileUpload")
    public  String fileUpload(@RequestParam("file")MultipartFile file) throws IOException, MyException {
        //文件上传
        //返回文件上传后的路径
        String configFile = this.getClass().getResource("/tracker.conf").getFile();
        ClientGlobal.init(configFile);
        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer=trackerClient.getTrackerServer();
        StorageClient storageClient=new StorageClient(trackerServer,null);

    /*    String filename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(filename);
*/
        String filename=    file.getOriginalFilename();
        String extName = FilenameUtils.getExtension(filename);


        String[] upload_file = storageClient.upload_file(file.getBytes(), extName, null);
        String path="http://192.168.109.135";
        for (int i = 0; i < upload_file.length; i++) {
            String s = upload_file[i];
            System.out.println("s = " + s);
            path+="/"+s;
        }
        System.out.println(path);

        return path;
    }




    @RequestMapping("/baseSaleAttrList")
    public List<PmsBaseSaleAttr> baseSaleAttrList(){
        List<PmsBaseSaleAttr> pmsBaseSaleAttrs = spuService.baseSaleAttrList();
        return pmsBaseSaleAttrs;
    }

    @RequestMapping("/saveSpuInfo")
    public String saveSpuInfo(@RequestBody  PmsProductInfo pmsProductInfo){
        if (pmsProductInfo!=null){
            spuService.saveSpuInfo(pmsProductInfo);
            return "success";
        }else {
            return "error";
        }

    }
    @RequestMapping("/spuSaleAttrList")
    public  List<PmsProductSaleAttr> spuSaleAttrList(Long spuId){
        List<PmsProductSaleAttr> spuSaleAttrList =   spuService.spuSaleAttrList(spuId);
        return spuSaleAttrList;
    }

    @RequestMapping("/spuImageList")
    public  List<PmsProductImage> spuImageList(Long spuId){
        List<PmsProductImage> spuImageList= spuService.spuImageList(spuId);
        return spuImageList;
    }

}
